#include "qobjectmanager.h"

#include "qobjectproperty.h"
#include "propertycreator.h"
#include "qproperty.h"

#include "../../lib/corelib/qscriptparameter.h"
#include "../../lib/shared/qobjecthost.h"

QList<PropertyCreator*> QObjectManager::m_creators;

void qRegPropertyCreator(PropertyCreator *creator)
{
    QObjectManager::m_creators.removeAll(creator);
    QObjectManager::m_creators.append(creator);
}


QObjectManager::QObjectManager(AbstractCore *core, QObject *parent):
    QObject(parent),
    m_core(core)
{
    qRegPropertyType("QRectProperty","geometry");
    qRegPropertyType("QSizeProperty","size");
    qRegPropertyType("QByteProperty","pageName");
    qRegPropertyType("QBoolProperty","enabled");
    qRegPropertyType("QIntProperty","Z-Value");
    qRegPropertyType("QFontProperty","font");
    qRegPropertyType("QEnumProperty","focusPolicy");
    qRegPropertyType("QCursorProperty","cursor");
    qRegPropertyType("QByteProperty","objectName");
    qRegPropertyType("QComboItemsProperty","comboBox_items");
    qRegPropertyType("QIntProperty","currentIndex");
    qRegPropertyType("QIntProperty","maxVisibleItems");
    qRegPropertyType("QBoolProperty","editable");
    qRegPropertyType("QBoolProperty","readOnly");
    qRegPropertyType("QBoolProperty","frame");
    qRegPropertyType("QIntProperty","maxLength");
    qRegPropertyType("QBoolProperty","auto-running");
    qRegPropertyType("QEnumProperty","orientation");
    qRegPropertyType("QIntProperty","value");
    qRegPropertyType("QByteProperty","text");
    qRegPropertyType("QByteProperty","toolTip");
    qRegPropertyType("QBoolProperty","visible");
    qRegPropertyType("QBoolProperty","checked");
    qRegPropertyType("QIntProperty","margin");
    qRegPropertyType("QBoolProperty","wordWrap");
    qRegPropertyType("QEnumProperty","mode","QLCDNumber");
    qRegPropertyType("QIntProperty","numDigits");
    qRegPropertyType("QIntProperty","maximum");
    qRegPropertyType("QIntProperty","minimum");
    qRegPropertyType("QFloatProperty","value","QDoubleSpinBox");
    qRegPropertyType("QFloatProperty","maximum","QDoubleSpinBox");
    qRegPropertyType("QFloatProperty","minimum","QDoubleSpinBox");
    qRegPropertyType("QIntProperty","decimals");
    qRegPropertyType("QDateProperty","date");
    qRegPropertyType("QTimeProperty","time");
    qRegPropertyType("QDateTimeProperty","dateTime");
    qRegPropertyType("QBoolProperty","textVisible");
    qRegPropertyType("QBoolProperty","invertedAppearance");
    qRegPropertyType("QEnumProperty","frameShadow");
    qRegPropertyType("QEnumProperty","frameShape");
    qRegPropertyType("QIntProperty","lineWidth");
    qRegPropertyType("QIntProperty","midLineWidth");
    qRegPropertyType("QMemeryValueProperty","memeryValue");

    qRegPropertyType("QScriptProperty","mousePressEvent");
    qRegPropertyType("QScriptProperty","mouseMoveEvent");
    qRegPropertyType("QScriptProperty","mouseRelessEvent");
    qRegPropertyType("QScriptProperty","mouseDbClickEvent");

    qRegPropertyType("QScriptProperty","clicked");
    qRegPropertyType("QScriptProperty","editingFinished");
    qRegPropertyType("QScriptProperty","currentIndexChanged");
    qRegPropertyType("QScriptProperty","valueChanged");
    qRegPropertyType("QScriptProperty","timeChanged");
    qRegPropertyType("QScriptProperty","dateChanged");
    qRegPropertyType("QScriptProperty","dateTimeChanged");


    qRegPropertyType("QColorProperty","color");
    qRegPropertyType("QColorProperty","textColor");
    qRegPropertyType("QColorProperty","disabled textColor");
    qRegPropertyType("QColorProperty","hover textColor");
    qRegPropertyType("QColorProperty","pressed textColor");
    qRegPropertyType("QImageProperty","image");
    qRegPropertyType("QImageProperty","disabled image");
    qRegPropertyType("QImageProperty","hover image");
    qRegPropertyType("QImageProperty","pressed image");
    qRegPropertyType("QPaddingProperty","padding");


    tagScriptFuncInfo info;
    tagScriptParaInfo p;

    info.m_userID="mouseEvent";
    info.m_FuncInfo="";
    p.m_name="_button";
    p.m_type="integer";
    p.m_info="Left=1 Right=2 Mid=4";
    info.m_para.append(p);
    p.m_name="_x";
    p.m_type="integer";
    p.m_info="";
    info.m_para.append(p);
    p.m_name="_y";
    p.m_type="integer";
    p.m_info="";
    info.m_para.append(p);
    QScriptParameter::insertFuncInfo(info);

    info.m_para.clear();
    info.m_userID="currentIndexChanged";
    info.m_FuncInfo="";
    p.m_name="_index";
    p.m_type="integer";
    p.m_info="Current Index";
    info.m_para.append(p);
    QScriptParameter::insertFuncInfo(info);

    info.m_para.clear();
    info.m_userID="editingFinished";
    info.m_FuncInfo="";
    p.m_name="_text";
    p.m_type="string";
    p.m_info="";
    info.m_para.append(p);
    QScriptParameter::insertFuncInfo(info);

    info.m_para.clear();
    info.m_userID="valueChanged";
    info.m_FuncInfo="";
    p.m_name="_value";
    p.m_type="integer";
    p.m_info="New Value";
    info.m_para.append(p);
    QScriptParameter::insertFuncInfo(info);

    info.m_para.clear();
    info.m_userID="valueChanged_double";
    info.m_FuncInfo="";
    p.m_name="_value";
    p.m_type="double";
    p.m_info="New Value";
    info.m_para.append(p);
    QScriptParameter::insertFuncInfo(info);

    info.m_para.clear();
    info.m_userID="clicked_bool";
    info.m_FuncInfo="";
    p.m_name="_checked";
    p.m_type="string";
    p.m_info="checked=\"true\"  unchecked=\"false\"";
    info.m_para.append(p);
    QScriptParameter::insertFuncInfo(info);


    info.m_para.clear();
    info.m_userID="timeChanged";
    info.m_FuncInfo="";
    p.m_name="_hour";
    p.m_type="integer";
    p.m_info="";
    info.m_para.append(p);
    p.m_name="_minute";
    p.m_type="integer";
    p.m_info="";
    info.m_para.append(p);
    p.m_name="_second";
    p.m_type="integer";
    p.m_info="";
    info.m_para.append(p);
    QScriptParameter::insertFuncInfo(info);

    info.m_para.clear();
    info.m_userID="dateChanged";
    info.m_FuncInfo="";
    p.m_name="_year";
    p.m_type="integer";
    p.m_info="";
    info.m_para.append(p);
    p.m_name="_month";
    p.m_type="integer";
    p.m_info="";
    info.m_para.append(p);
    p.m_name="_day";
    p.m_type="integer";
    p.m_info="";
    info.m_para.append(p);
    QScriptParameter::insertFuncInfo(info);

    info.m_para.clear();
    info.m_userID="dateTimeChanged";
    info.m_FuncInfo="";
    p.m_name="_year";
    p.m_type="integer";
    p.m_info="";
    info.m_para.append(p);
    p.m_name="_month";
    p.m_type="integer";
    p.m_info="";
    info.m_para.append(p);
    p.m_name="_day";
    p.m_type="integer";
    p.m_info="";
    info.m_para.append(p);
    p.m_name="_hour";
    p.m_type="integer";
    p.m_info="";
    info.m_para.append(p);
    p.m_name="_minute";
    p.m_type="integer";
    p.m_info="";
    info.m_para.append(p);
    p.m_name="_second";
    p.m_type="integer";
    p.m_info="";
    info.m_para.append(p);
    QScriptParameter::insertFuncInfo(info);
}

QObjectProperty *QObjectManager::getObjectProperty(QObject *object)
{
    QObjectProperty *ret=m_objectPropertys.value(object);

    if(ret==NULL)
    {
        ret=new QObjectProperty(this,object,this);
        connect(object,SIGNAL(destroyed()),this,SLOT(objectDestory()));
        connect(ret,SIGNAL(changed(QProperty*)),this,SLOT(propertyChanged(QProperty*)));
        m_objectPropertys.insert(object,ret);
    }
    return ret;
}

void QObjectManager::objectDestory()
{
    QObject *obj=sender();
    QObjectProperty *pro=m_objectPropertys.value(obj);
    if(pro!=NULL)
    {
        delete pro;
        m_objectPropertys.remove(obj);
    }
}

QProperty* QObjectManager::createProperty(QString name, QString className)
{
    PropertyCreator *creator;
    QProperty* pro;
    for(int i=0;i<m_creators.size();i++)
    {
        creator=m_creators.at(i);
        pro=creator->createProperty(name,className);
        if(pro!=NULL)
        {
            pro->setCore(m_core);
            return pro;
        }
    }
    return NULL;
}

QWidget* QObjectManager::createEditor(QWidget *parent, QProperty *property)
{
    PropertyCreator *creator;
    QWidget* wid;
    for(int i=0;i<m_creators.size();i++)
    {
        creator=m_creators.at(i);
        wid=creator->createEditor(parent,property);
        if(wid!=NULL)
        {
            return wid;
        }
    }
    return NULL;
}

void QObjectManager::propertyChanged(QProperty *pro)
{
    emit changed((QObjectProperty*)sender(),pro);
}
